package com.gistone.commis.api.restController;

import com.gistone.commis.constant.Status;
import com.gistone.commis.database.GetBySqlMapper;
import com.gistone.commis.dto.Result;
import com.gistone.commis.entity.*;
import com.gistone.commis.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 * Created by wangfan on 2017/6/11.
 */

@RestController("apiImageController")
@RequestMapping("api/image")
public class ImageController {
    private static final Logger logger = LoggerFactory.getLogger(ImageController.class);
    @Value("${app.uploadPath}")
    private String path;
    @Autowired
    CommunityInfoMapper communityInfoMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    FundMapper fundMapper;
    @Autowired
    FundChangeMapper fundChangeMapper;
    @Autowired
    private GetBySqlMapper getBySqlMapper;






    @RequestMapping(value = "getImagePathForCom", method = RequestMethod.POST)
    public Result getImagePathForCom(Integer communityId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CommunityInfo ci =new CommunityInfo();
        ci.setCommunityId(communityId);
        List<CommunityInfo> list =communityInfoMapper.selectSelective(ci);
        if(list.size()>0){
            CommunityInfo communityInfo =list.get(list.size()-1);
            Map m =new HashMap<>();
            m.put("signImg",communityInfo.getReserveA());
            m.put("yinzhangImg",communityInfo.getReserveB());
            return  new Result(Status.SUCCESS,"",m);

        }else{
            return  new Result(Status.ERROR,"no","");
        }

    }


    @RequestMapping(value = "getImagePathForFund", method = RequestMethod.POST)
    public Result getImagePathForFund(Integer fundId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Map> data = null;
        try {
            String sql = "select tb_pp_change.*,tb_user.image,tb_user.user_type_id from tb_pp_change,tb_user where   tb_user.user_id =tb_pp_change.current_auditor_id " +
                    "AND  tb_pp_change.pp_id =" + "'" + fundId + "'";
            data = new ArrayList<>();
            List<Map> results= this.getBySqlMapper.findRecords(sql);
            for (Map result : results) {
                Integer userTypeId =(Integer)result.get("user_type_id");
                if(userTypeId==3||userTypeId==6||userTypeId==7||userTypeId==8||userTypeId==9){
                    data.add(result);
                }

            }

        } catch (Exception e) {
            return  new Result(Status.ERROR,"","");
        }

        return  new Result(Status.SUCCESS,"success",data);


    }

    @RequestMapping(value = "getImagePathForOS", method = RequestMethod.POST)
    public Result getImagePathForOS(Integer osId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Map> data = null;
        try {
            String sql = "select tb_office_supplies_change.*,tb_user.image,tb_user.user_type_id from tb_office_supplies_change,tb_user where   tb_user.user_id =tb_office_supplies_change.current_auditor_id " +
                    "AND  tb_office_supplies_change.supplies_id =" + "'" + osId + "'";
            data = new ArrayList<>();
            List<Map> results= this.getBySqlMapper.findRecords(sql);
            for (Map result : results) {
                Integer userTypeId =(Integer)result.get("user_type_id");
                if(userTypeId==3||userTypeId==6||userTypeId==7||userTypeId==8||userTypeId==9){
                    data.add(result);
                }

            }

        } catch (Exception e) {
            return  new Result(Status.ERROR,"","");
        }

        return  new Result(Status.SUCCESS,"success",data);


    }


}
